package com.keyin.widget;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnCancelListener;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.keyin.PastPeridocalsActivity;
import com.keyin.R;
import com.keyin.protocol.CacheImageProtocol;
import com.keyin.protocol.DownloadMagazine;
import com.keyin.protocol.MagazineBean;
import com.keyin.protocol.Magazines;
import com.keyin.protocol.PastPeriodicals;
import com.keyin.protocol.NewPeriodicals;
import com.keyin.protocol.Protocol;
import com.keyin.protocol.Protocol.OnJsonProtocolResult;
import com.keyin.protocol.ProtocolHelper;

public class MagazineListWidget extends MainWidgetBase {

	private View container;
	private ListView listView;

	private Magazines magazines;

	public MagazineListWidget(Activity context) {
		super(context);

		parent = context;
	}

	@Override
	public void onNewIntent(Intent i) {
		if (container == null) {
			
			container = View.inflate(getContext(), R.layout.v_magazine_list,
					null);
			addView(container);
			
			progressDialog = ProgressDialog.show(parent, "请稍候", "正在获取数据", true);
			progressDialog.setCancelable(true);
			progressDialog.setProgressStyle(android.R.style.Theme_Translucent);
			progressDialog.setOnCancelListener(new OnCancelListener() {
				@Override
				public void onCancel(DialogInterface d) {
				}
			});
			Protocol p = ProtocolHelper.getMagazineList(getContext());
			p.startTrans(new OnJsonProtocolResult(Magazines.class) {

				@Override
				public void onResult(Object o) {
					handler.sendMessage(Message.obtain(handler, 1, o));
				}

				@Override
				public void onException(Exception e) {
					handler.sendEmptyMessage(2);
					e.printStackTrace();
				}
			});
		}
	}

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			if (msg.what == 1) {
				Magazines mzs = (Magazines) msg.obj;
				refreshUI(mzs);
			} else if (msg.what == 2) {
				progressDialog.dismiss();
			}
		};
	};

	private void refreshUI(Magazines nm) {
		progressDialog.dismiss();
		magazines = nm;
		// for (int i = 0; i < nm.magazines.size(); i++) {
		// NewMagazine m = nm.magazines.get(i);
		// CacheImageProtocol imgP = new CacheImageProtocol(
		// getApplicationContext(), m.cover_img);
		// imgP.startTrans(new Object());
		// }

		listView = (ListView) findViewById(R.id.magazine_list);
		listView.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View view = convertView;
				if (view == null) {
					view = View.inflate(getContext(), R.layout.v_list_item,
							null);
				}

				MagazineBean bean = magazines.magazines.get(position);

				ImageView img = (ImageView) view.findViewById(R.id.cover);
				String imgUrl = bean.cover_img;
				if (imgUrl != null) {
					CacheImageProtocol imgProtocol = new CacheImageProtocol(
							getContext(), imgUrl);
					imgProtocol.startTrans(img);
				}

				TextView name = (TextView) view.findViewById(R.id.name);
				name.setText(bean.name);

				TextView des = (TextView)view.findViewById(R.id.intro);
				des.setText(bean.description);
				
				TextView date = (TextView)view.findViewById(R.id.date);
				date.setVisibility(View.GONE);
				return view;
			}

			@Override
			public long getItemId(int position) {
				return 0;
			}

			@Override
			public Object getItem(int position) {
				return null;
			}

			@Override
			public int getCount() {
				return magazines.magazines.size();
			}
		});
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String magazine_id = magazines.magazines.get(position).magazine_id;
				
				Intent i = new Intent(MagazineListWidget.this.parent, PastPeridocalsActivity.class);
				i.putExtra("magazineId", magazine_id);
				MagazineListWidget.this.parent.startActivity(i);
			}
		});
	}
}
